﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace libcontrol
{
    //通讯命令编号
   public enum ComCmd
    {
        CMD_PROGRAM =200,			// 
        CMD_SET_DC_MOTOR=201,			//
        CMD_SET_STEP_MOTOR=202,			//
        CMD_GET_VERSION=203,			//
        CMD_GET_SENSOR=230,		  //
        CMD_GET_PARAMETER=206,			// 
        CMD_SET_PARAMETER=207,			// 
        CMD_GET_STEP_INFO = 210,			// 
        CMD_SHARE_MEM_CMD,// ESULT = 211,     //
        CMD_GET_ALL_OP_TIME = 212,     //
        CMD_SHARE_PC = 214,
        CMD_WALK_INTERMSG =215,
        CMD_DO_ACTION =216,			//
        CMD_GET_RESULT	= 223,		  //
        CMD_GET_ERROR_INFO = 224,			//
        CMD_GET_STEP_SEQ = 225,			//
        CMD_GET_DATA	= 226,			//
        CMD_SET_DATA = 227,			//
        CMD_GET_BOARDNAME = 237,			//
        CMD_GET_MCU_DATA = 238,			//
        CMD_GOTO_IAP    = 242,			//
        CMD_GET_TEMPTURE = 245,			//
        CMD_WALK_DEVICE	= 246,          //
        CMD_RETRY_CMD = 248,
        CMD_ERROR_CANCEL = 249,
    };

    enum EnumCmd
    {
        Cmd_EnumAction,//0
        Cmd_EnumSensor,//1
        Cmd_EnumStepMotor,//2
        Cmd_EnumDMotor,//3
        Cmd_EnumActionParam,//4
        Cmd_EnumCount,//5
        Cmd_EnumFlashParam,//6
    };


    class libcom
    {
        const string strDllFile = "libcom.dll";
        [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall)]
   
        public static extern int Connect(int nID, int type, string strAddr);

        [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall)]
        public static extern int SendCmd(int nID,
            [Out, MarshalAs(UnmanagedType.LPArray)]byte[] SendBuffer, int SendLen, 
            [Out, MarshalAs(UnmanagedType.LPArray)]byte[] RecvBuffer, ref int RecvLen);
        public static object CommLock = new object();
        public static int SendCommand(int nID, byte[] SendBuffer, int SendLen,byte[] RecvBuffer, ref int RecvLen)
        {
            lock (CommLock)
            {
                return SendCmd(nID,SendBuffer,SendLen,RecvBuffer,ref RecvLen);
            }
        }

        [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall)]
        public static extern int CloseCom();
    }
}
